package com.etong.citymap.util;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;

import androidx.core.app.ActivityCompat;

import com.etong.citymap.db.util.CityApplication;
import com.etong.citymap.util.log.LogDetailUtil;
import com.supermap.data.Point2D;

import java.util.List;

/**
 * 地图相关处理工具类
 */
public class MapInfoUtil {

	/**
	 * 获取当前位置信息,包括高度
	 * @param context
	 * @return
	 */
	public static Location getLocation(Context context) {
		// 获取位置管理服务
		LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
		if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
				&& ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
			return null;
		}
		List<String> providers = locationManager.getProviders(true);
		Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);// 通过GPS获取位置
		LogDetailUtil.d("通过MapInfoUtil.getLocation(Context context) 获得的定位信息是：" + location);
		return location;
	}

	/**
	 * 获得二维平面定位信息
	 * @param context
	 * @return
	 */
	public static Point2D get2DLocation(Context context) {
		Location location =  getLocation(context);
		if(null == location){
			return null;
		}
		Point2D point2D = new Point2D();
		point2D.setX(location.getLongitude());
		point2D.setY(location.getLatitude());
		return point2D;
	}

	/**
	 * 获得二维平面定位信息
	 * @param context
	 * @return
	 */
	public static synchronized Point2D get2DLocation4Supermap(Context context) {
//		Location location =  getLocation(context);//这种方式无法激活卫星,增加了locationManager.getProviders(true);可以了
		Location tempLocation = CityApplication.getLocation();
		if(null == tempLocation){
			return null;
		}
		Location location = new Location(tempLocation);

		Gps gps4Bd = PositionUtil.gps84_To_Bd09(location.getLatitude(), location.getLongitude());
		location.setLatitude(gps4Bd.getWgLat());
		location.setLongitude(gps4Bd.getWgLon());

		CoordinateConversion conversion  = new CoordinateConversion();
		Point2D p1 = conversion.latLon2UTM(location.getLatitude(),location.getLongitude());
		p1.setX(p1.getX()-143895-13);
		p1.setY(p1.getY()+190+10);
		return p1;
	}

}
